/*
 * @Author: monet hanxu@huaun.com
 * @Date: 2023-09-13 09:52:40
 * @LastEditors: zhangyonggui
 * @LastEditTime: 2023-11-28 16:22:06
 * @FilePath: /h-radar-expose/store/expose.js
 * @Description:
 */
import $axios from '../services/http';

const state = () => ({
  exposed: '/exposed',
  attack: '/attack',
  saasExposed: '/saas-exposed',
  advancedSearchOpts: {},
  mappingStatus: 0,
  searchData: {},
});

const mutations = {
  SET_ADVANCED_SEARCH_OPTS(state, data) {
    state.advancedSearchOpts = data;
  },
  SET_MAPPING_STATUS(state, data) {
    state.mappingStatus = data;
  },
  setSearchData(state, data) {
    state.searchData = data;
  },
};
const actions = {
  async labelCategoryList({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}/api/tag-category/category-and-tag-tree`,
      data,
    );
  },
  async pageCom({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}/api/nanjing/com-tree`,
      data,
    );
  },
  async selectedList({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}/api/nanjing/list-selected-value`,
      data,
    );
  },
  async calculateRichness({ state, rootState }, data) {
    return await $axios.get(
      `${rootState.routeServiceV2}/api/nanjing/calculate-richness`,
    );
  },
  // 分页查询租户标签库
  async queryTagList({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}${state.saasExposed}/tenant-tag-library/list`,
      data,
      { headers: { showLoading: false } },
    );
  },
  // 设置高级搜索选项信息
  async setAdvancedSearchOpts({ state, commit, rootState }) {
    const { code, data } = await $axios.get(
      `${rootState.routeServiceV2}${state.exposed}/high-search/parameter-dict`,
    );

    const opts = code === 0 ? data : {};
    const commonTags = [
      'company',
      'tag',
      'is_alive',
      'data_status',
      'exposure_risk',
      'vulnerability',
      'company_only',
      'tag_company',
      'tag_company_only',
    ];

    for (const key in opts) {
      opts[key].forEach((el) => {
        if (commonTags.includes(el.value)) {
          el.signValue = `${el.value}`;
        } else {
          el.signValue = `${key}.${el.value}`;
        }
      });
    }
    // console.log(opts, 222222)
    commit('SET_ADVANCED_SEARCH_OPTS', opts);
  },
  // 资产误报
  async markAsset({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}${state.exposed}/asset/mark-asset-misinformation`,
      data,
    );
  },
  // 批量资产误报
  async batchMarkAsset({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}${state.exposed}/asset/mark-asset-misinformation-batch`,
      data,
      { headers: { showLoading: false } },
    );
  },
  // 获取配置的暴露面类型集合
  async getExposedTypes({ state, rootState }, data) {
    return await $axios.get(
      `${rootState.routeServiceV2}${state.exposed}/get-exposed-types`,
    );
  },
  // 获取左侧数据
  async getLeftList({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}${state.exposed}/aggregation`,
      data,
    );
  },
  // 获取列表数据
  async getList({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}${state.exposed}/search`,
      data,
    );
  },
  // 保存高级检索语句
  async addCondition({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}${state.exposed}/high-search/add-high-search-condition`,
      data,
    );
  },


  // 高级检索语句-分页查询
  async getCondition({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}${state.exposed}/high-search/page-search-record`,
      data,
    );
  },
  // 高级检索语句-删除
  async deleteCondition({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}${state.exposed}/high-search/delete-search-condition`,
      data,
    );
  },
  // 高级检索语句-更新
  async editCondition({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}${state.exposed}/high-search/edit-search-condition`,
      data,
    );
  },
  // 启动测绘
  async startMapping({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.keywordService}/plotting/start`,
      data,
    );
  },
  // 策略列表
  async getStrategyList({ state, rootState }, data) {
    return await $axios.get(
      `${rootState.strategyService}/strategy/list`,
    );
  },
  // 任务策略输入框
  async getStrategyInput({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}/workflow/first/input`,
      data,
    );
  },
  // 策略详情
  async getStrategyDetail({ state, rootState }, data) {
    return await $axios.get(
      `${rootState.strategyService}/strategy/info/${data}`,
    );
  },
  // 任务计划-数据标记
  async getTagList({ state, rootState }, data) {
    return await $axios.get(
      `${rootState.strategyService}/strategy/task-tag/list/${data}`,
    );
  },
  // 任务下发保存接口
  async taskDistribute({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}/workflow/task/distribute`,
      data,
    );
  },
  // 根据选择条件获取资产总数
  async getAssetCount({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}/asset/count`,
      data,
    );
  },
};

export default {
  namespaced: true, // 有命名空间
  state,
  mutations,
  actions,
};
